﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SurveySystem.Core;
using System.Web.Security;
namespace SurveySystem.Web
{
    public partial class Settings : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");

            if (!IsPostBack)
            {
                
                User user = UsersBLL.GetUser(User.Identity.Name);
                tbEmail.Text = user.email;
                tbFullName.Text = user.fullName;
                tbPhoneNumber.Text = string.IsNullOrEmpty(user.mobile) ? "" : user.mobile;

                chkEmailOnExpiration.Checked = user.getEmailOnExpiration;
                chkEmailOnNewSurvey.Checked = user.getEmailOnNewSurvey;
                chkSmsOnExpiration.Checked = user.getSmsOnExpiration;
                chkSmsOnNewSurvey.Checked = user.getSmsOnNewSurvey;
            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            MembershipUser usr = Membership.GetUser();
            usr.Email = tbEmail.Text;
            Membership.UpdateUser(usr);

            User user = new User();
            user.email = tbEmail.Text;
            user.fullName = tbFullName.Text;
            user.getEmailOnExpiration = chkEmailOnExpiration.Checked;
            user.getEmailOnNewSurvey = chkEmailOnNewSurvey.Checked;
            user.getSmsOnExpiration = chkSmsOnExpiration.Checked;
            user.getSmsOnNewSurvey = chkSmsOnNewSurvey.Checked;
            user.mobile = tbPhoneNumber.Text;

            UsersBLL.UpdateUser(User.Identity.Name, user);
            lblUpdateSuccess.Visible = true;
        }

        protected void userDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.InputParameters[0] = User.Identity.Name;
        }

    }
}
